#Region " Imports "

Imports System
Imports System.Data
Imports BlazeApps.Library.Template

#End Region

Partial Class Usercontrols_Admin_Template
    Inherits UserControlBase

#Region " Enums, Memebers and Properties "

    Private Enum Columns
        Edit
        PlaceholderNumber
        FieldName
        FieldType
        FieldHeight
        FieldWidth
        EditorType
    End Enum

    Protected TemplateId As Guid

    Protected Property PlaceHolderNumber() As Integer
        Get
            Return GetIntegerFromVS("PlaceHolderNumber")
        End Get
        Set(ByVal value As Integer)
            ViewState.Add("PlaceHolderNumber", value)
        End Set
    End Property

#End Region

#Region " Page Events "

    Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
        Me.MessageLabel = Me.uxMessagesLabel
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        SetPageTitle("Templates")

        GetQueryStringValues()

        If Not IsPostBack Then

            If Not TemplateId = Guid.Empty Then
                LoadTemplatHeader()
                LoadTemplateGrid(-1)
            End If

        End If

        Me.uxTemplatePanel.Visible = ShowControls(SecurityMask.Template, True)

    End Sub

#End Region

#Region " Save Data "

    Protected Sub SaveTemplateHeader()
        Dim Template As TemplateDetail = TemplateDetail.GetTemplateDetail(GetGuidFromQueryString("tid"))

        Template.templateid = TemplateId
        Template.templatename = Me.uxTemplateNameTextBox.Text.ToString.Trim
        Template.numberofplaceholders = CType(Me.uxPlaceholdersTextBox.Text.Trim, Integer)
        Template.Save()

    End Sub

#End Region

#Region " Load Data "

    Protected Sub LoadTemplatHeader()
        Dim TemplateItems As TemplateDetail = TemplateDetail.GetTemplateDetail(TemplateId)

        Me.uxTemplateNameTextBox.Text = TemplateItems.templatename
        Me.uxPlaceholdersTextBox.Text = TemplateItems.numberofplaceholders

    End Sub

    Protected Sub LoadTemplateGrid(ByVal Index As Integer)
        Dim TemplateList As TemplateParent = TemplateParent.GetTemplateParent(TemplateId)

        If TemplateList.Count = CType(Me.uxPlaceholdersTextBox.Text, Integer) Then
            Me.uxAddFieldPanel.Visible = False
        Else
            Me.uxAddFieldPanel.Visible = True
        End If

        With Me.uxTemplateGridView
            .SelectedIndex = Index
            .DataSource = TemplateList
            .DataBind()
        End With

    End Sub

#End Region

#Region " Form Controls Events "

    Protected Sub uxAddButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles uxAddButton.Click
        Dim TemplateItem As TemplateChildDetail = TemplateChildDetail.NewTemplateChildDetail

        TemplateItem.templateid = TemplateId
        TemplateItem.fieldname = Me.uxAddFieldNameTextBox.Text.Trim
        TemplateItem.fieldtype = 1 'Future Use
        TemplateItem.fieldheight = CType(Me.uxAddFieldHeightTextBox.Text, Integer)
        TemplateItem.fieldwidth = CType(Me.uxAddFieldWidthTextBox.Text, Integer)
        TemplateItem.editortype = 1 'Future Use
        TemplateItem.ShowName = Me.uxAddShowNameCheckBox.Checked
        TemplateItem.Save()

        LoadTemplateGrid(-1)

    End Sub

    Protected Sub uxSaveButton_Click(ByVal sender As Object, ByVal e As System.EventArgs)

        If TemplateId = Guid.Empty Then
            CreateNewPage()
        Else
            SavePage()
        End If

    End Sub

    Protected Sub uxSaveEditButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles uxSaveEditButton.Click
        If PlaceHolderNumber = "0" Then
            Dim TemplateNewItem As TemplateChildDetail = TemplateChildDetail.NewTemplateChildDetail

            TemplateNewItem.templateid = TemplateId
            TemplateNewItem.placeholdernumber = 1
            TemplateNewItem.fieldname = Me.uxEditFieldNameTextBox.Text
            TemplateNewItem.fieldtype = 1 'Future Use
            TemplateNewItem.fieldheight = CType(Me.uxEditFieldHeightTextBox.Text, Integer)
            TemplateNewItem.fieldwidth = CType(Me.uxEditFieldWidthTextBox.Text, Integer)
            TemplateNewItem.editortype = 1 'Future Use
            TemplateNewItem.ShowName = Me.uxEditShowNameCheckBox.Checked
            TemplateNewItem.Save()
        Else
            Dim TemplateItem As TemplateChildDetail = TemplateChildDetail.GetTemplateChildDetail(GetGuidFromQueryString("tid"), PlaceHolderNumber)

            TemplateItem.templateid = TemplateId
            TemplateItem.placeholdernumber = PlaceHolderNumber
            TemplateItem.fieldname = Me.uxEditFieldNameTextBox.Text
            TemplateItem.fieldtype = 1 'Future Use
            TemplateItem.fieldheight = CType(Me.uxEditFieldHeightTextBox.Text, Integer)
            TemplateItem.fieldwidth = CType(Me.uxEditFieldWidthTextBox.Text, Integer)
            TemplateItem.editortype = 1 'Future Use
            TemplateItem.ShowName = Me.uxEditShowNameCheckBox.Checked
            TemplateItem.Save()
        End If
        Me.uxEditFieldPanel.Visible = False
        LoadTemplateGrid(-1)
    End Sub

    Protected Sub uxCancelEditButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles uxCancelEditButton.Click
        Me.uxEditFieldPanel.Visible = False
        LoadTemplateGrid(-1)
    End Sub

    Protected Sub uxNewPageButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles uxNewPageButton.Click
        Response.Redirect(String.Format("{0}.aspx?pid=tpl_template", AppPage))
    End Sub

    Protected Sub uxTemplateGridView_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles uxTemplateGridView.RowDataBound
        Dim CL As Label = e.Row.FindControl("uxTemplateUseCountLabel")

        If e.Row.RowType = DataControlRowType.DataRow Then
            If CType(CL.Text.ToString.Trim, Integer) > 0 Then
                Me.uxSaveButton.Enabled = False
                Me.uxAddButton.Enabled = False
                Me.uxTemplateUsedLabel.Visible = True
                Me.uxTemplateUsedLabel.Text = "This template is actively being used.  You will not be able to remove or add fields, only editing will be allowed."
            End If
        End If
    End Sub

    Protected Sub SecureItem(ByVal sender As Object, ByVal e As CommandEventArgs)
        Dim Item As String() = Split(e.CommandArgument.ToString, "|")

        Me.uxSecurityModalPopupExtender.Show()

        uxSecurity.ItemGuid = ConvertToGuid(Item(0).ToString.Trim)
        uxSecurity.ItemName = Item(1).ToString.Trim
        uxSecurity.BindDataItems()
        uxSecurity.LoadItemName()

    End Sub

    Protected Sub EditItem(ByVal sender As Object, ByVal e As CommandEventArgs)
        Dim Item As String() = Split(e.CommandArgument.ToString, "|")

        PlaceHolderNumber = Convert.ToInt32(Item(1))
        Me.uxEditFieldPanel.Visible = True

        Dim TemplateItem As TemplateChildDetail = TemplateChildDetail.GetTemplateChildDetail(GetGuidFromQueryString("tid"), PlaceHolderNumber)

        Me.uxEditFieldNameTextBox.Text = TemplateItem.fieldname
        Me.uxEditFieldHeightTextBox.Text = TemplateItem.fieldheight
        Me.uxEditFieldWidthTextBox.Text = TemplateItem.fieldwidth

    End Sub

#End Region

#Region " Helpers and Methods "

    Private Sub CreateNewPage()
        Dim Template As TemplateDetail = TemplateDetail.NewTemplateDetail

        Template.templatename = Me.uxTemplateNameTextBox.Text.ToString.Trim
        Template.numberofplaceholders = CType(Me.uxPlaceholdersTextBox.Text.Trim, Integer)
        Template.Save()

        Response.Redirect(String.Format("{0}.aspx?pid=tpl_template&tid={1}", AppPage, Template.templateid.ToString))
    End Sub

    Private Sub SavePage()
        SaveTemplateHeader()
        LoadTemplatHeader()
        LoadTemplateGrid(-1)
    End Sub

    Private Sub GetQueryStringValues()
        TemplateId = GetGuidFromQueryString("tid")
    End Sub
#End Region

End Class
